package com.paic.custserv.service.impl;


import static com.paic.custserv.common.Constant.WORK_ORDER_HANDLER_QUEUE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.aspectj.util.LangUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.paic.custserv.common.Constant;
import com.paic.custserv.common.enums.WorkOrderStatus;
import com.paic.custserv.domain.AppUser;
import com.paic.custserv.domain.CussAttachment;
import com.paic.custserv.domain.CussSystemConfig;
import com.paic.custserv.domain.CussWorkOrder;
import com.paic.custserv.domain.CussWorkOrderDealflow;
import com.paic.custserv.domain.Page;
import com.paic.custserv.domain.UserVO;
import com.paic.custserv.mapper.CussAttachmentMapper;
import com.paic.custserv.mapper.CussSystemConfigMapper;
import com.paic.custserv.mapper.CussWorkOrderDealflowMapper;
import com.paic.custserv.mapper.CussWorkOrderMapper;
import com.paic.custserv.mapper.CussWorkOrderTypeMapper;
import com.paic.custserv.service.ICussWorkOrderService;
import com.paic.custserv.utils.AuthenticationUtils;
import com.paic.custserv.utils.PropertiesHolder;
import com.paic.custserv.utils.redis.RedisCacheService;
import com.paic.custserv.web.controller.vo.CussWorkOrderQueryVO;

@Service("cussWorkOrderService")
public class CussWorkOrderServiceImpl implements ICussWorkOrderService {

	@Autowired
	private CussWorkOrderMapper cussWorkOrderMapper;
	@Autowired
	private CussSystemConfigMapper cussSystemConfigMapper;
	@Autowired
	private CussAttachmentMapper cussAttachmentMapper;	
	@Autowired
	private RedisCacheService redisCacheService;
	@Autowired
	private CussWorkOrderTypeMapper cussWorkOrderTypeMapper;	
	@Autowired
	private CussWorkOrderDealflowMapper cussWorkOrderDealflowMapper;
	

	private static final Logger logger = LoggerFactory.getLogger(CussWorkOrderServiceImpl.class);

	@Override
	public CussWorkOrder getCussWorkOrder(Integer idWorkOrder) throws Exception {
		return cussWorkOrderMapper.getCussWorkOrder(idWorkOrder);
	}

	@Override
	public List<CussWorkOrder> selectByParam(Page page,Map<String,Object> paramMap) {
		return cussWorkOrderMapper.selectWorkOrderByParam(page,paramMap);
	}

	@Override
	public CussSystemConfig selectByName(String name) {
		return cussSystemConfigMapper.selectByName(name);
	}

	@Override
	public void updateValueByName(String name,String value) {
		Map<String,String> paramMap =new HashMap<String,String>();
		paramMap.put("name", name);
		paramMap.put("value", value);
		cussSystemConfigMapper.updateValueByName(paramMap);
	}

	@Override
	public List<CussWorkOrder> selectListByPage(CussWorkOrderQueryVO vo) {
		return cussWorkOrderMapper.selectListByPage(vo);
	}

	@Override
	public int updateSelective(CussWorkOrder model) {
		return cussWorkOrderMapper.updateByPrimaryKeySelective(model);
	}	
	
	@Override
	public void insertCussWorkOrder(CussWorkOrder cussWorkOrder) throws Exception{
		CussWorkOrderDealflow cussWorkOrderDealflow=new CussWorkOrderDealflow();				
		cussWorkOrderDealflow.setCreator(cussWorkOrder.getCreator());
		cussWorkOrderDealflow.setCreatorType(0);//0 客服专员 1 客户
		
		//针对客服专员的录入，未选工单处理人时，则轮询分配处理人
		UserVO userVO=null;
		if(LangUtil.isEmpty(cussWorkOrder.getCurrOperatorUm())){				
			userVO=getWorkOrderHandler(userVO);
			cussWorkOrder.setCurrOperatorUm(userVO.getUserUMID());
			cussWorkOrderDealflow.setCurrOperatorUm(userVO.getUserUMID());
			cussWorkOrderDealflow.setOperateFlowLog("创建了工单，系统按分配规则分配给专员"+userVO.getUserUMID()+"进行处理");
		}
		else{
			cussWorkOrderDealflow.setCurrOperatorUm(cussWorkOrder.getCurrOperatorUm());
			cussWorkOrderDealflow.setOperateFlowLog("创建了工单，并分配给专员"+cussWorkOrder.getCurrOperatorUm()+"进行处理");
		}
		cussWorkOrderMapper.insertSelective(cussWorkOrder);
		
		cussWorkOrderDealflow.setIdWorkOrder(cussWorkOrder.getIdWorkOrder());
		cussWorkOrderDealflow.setCreatorName(AuthenticationUtils.getCurrentUser().getUsername());
		cussWorkOrderDealflow.setCreatorRole(AuthenticationUtils.getCurrentUser().getUserRole());
	//	cussWorkOrderDealflow.setPreWkordStatus(WorkOrderStatus.UnAllot.getValue());
		cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.Pending.getValue());
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);		
	}	
		
	/**
	 * 生成工单号中的自增部分code
	 * 
	 */
	public synchronized CussSystemConfig generateIncrCode()  throws Exception{
		CussSystemConfig  cussSysconf=cussSystemConfigMapper.selectByName("workOderIncrCode");
		updateValueByName("workOderIncrCode",""+(Integer.valueOf(cussSysconf.getValue()).intValue()+1));
		return cussSysconf;		
	}
	
	/**
	 * 针对客户录入，采用工单轮询的方式获取客服专员信息
	 * 获取客服专员方法
	 * @throws InterruptedException 
	 */
	@SuppressWarnings("unchecked")
	public synchronized UserVO getWorkOrderHandler(UserVO userVO) throws Exception{
		////获取所有客服专员信息		
    	Object queueObj=redisCacheService.get(WORK_ORDER_HANDLER_QUEUE);
    	logger.info("method getWorkOrderHandler  start queueObj= {}", queueObj); 
    	
    	if(queueObj==null)return userVO;
    	
    	BlockingQueue<UserVO> handlerQueue=(LinkedBlockingQueue<UserVO>)queueObj;
    	logger.info("method getWorkOrderHandler  start handlerQueue= {}", handlerQueue); 
		
    	if(	handlerQueue !=null&&(!handlerQueue.isEmpty())){
			//获取最近一次工单处理人账号
			CussSystemConfig  cussSysconf=cussSystemConfigMapper.selectByName("workOrderHandlerUM");				
			//找工单下一个处理人，并返回	
			if(cussSysconf ==null){		
				userVO=handlerQueue.poll();
				cussSysconf=new CussSystemConfig("workOrderHandlerUM",userVO.getUserUMID(),"最近一次工单处理人");
				cussSystemConfigMapper.insertCussSystemConfig(cussSysconf);
				handlerQueue.put(userVO);
		    }else{	
		    	//相等，取下一个元素，否则取头部元素
		    	for(int i=0;i<handlerQueue.size();i++){  
		    		UserVO temp=handlerQueue.poll();
		    		handlerQueue.put(temp);
		    				    		
		    		if((cussSysconf.getValue()).equals(temp.getUserUMID())){  
		    			userVO=handlerQueue.poll();  	    			
		    			handlerQueue.put(userVO);
		    			break;
		    		}
		    	  }
		    	  if(userVO==null){
		    		  userVO=handlerQueue.poll();
		    		  handlerQueue.put(userVO);
		    	  }	
		    	  updateValueByName("workOrderHandlerUM", userVO.getUserUMID());
		    }
		}
		logger.info("method getWorkOrderHandler result userVO= {}", userVO);
		redisCacheService.put(WORK_ORDER_HANDLER_QUEUE, 0, handlerQueue);			
		return userVO;
	} 
	
	@Override
	public void deleteCussWorkOrder(Integer idWorkOrder) {		
		cussWorkOrderMapper.deleteByPrimaryKey(idWorkOrder);
	}	
	
	@Override
	public void insertCussWorkOrderAndAttachment(CussWorkOrder cussWorkOrder,CussAttachment cussAttachment)  throws Exception{		
		insertCussWorkOrder(cussWorkOrder);
		cussAttachment.setRelatedEntityId(cussWorkOrder.getIdWorkOrder());
		insertCussAttachment(cussAttachment);
	}
	
	@Override
	public void updateByPrimaryKey(CussWorkOrder cussWorkOrder){
		cussWorkOrderMapper.updateByPrimaryKeySelective(cussWorkOrder);
	}
	
	@Override
	public int insertCussAttachment(CussAttachment record) {
		return cussAttachmentMapper.insertCussAttachment(record);
	}
	
	/**
	 * 调用接口，获取最新所有客服专员信息,按客服专员创建时间由远到近排序
	 * 定时任务
	 */
	public void getLatesAlltWorkOrderHandlers(){
		try{
			List<UserVO> handlerList=new ArrayList<UserVO>();//调用接口
			String custServExecutive=PropertiesHolder.getProperty("custServExecutive");
			
			String excetive[]= custServExecutive.split(",");
			UserVO userVOTemp=null;
			for(int i=0;i<excetive.length;i++){
				userVOTemp=new UserVO(excetive[i].split("@")[0],excetive[i].split("@")[1]);
				handlerList.add(userVOTemp);
			}
			
			logger.info("获取最新所有客服专员信息接口返回结果： result = {}", handlerList);
			
			//不存在客服专员信息
			if(handlerList==null||handlerList.size()==0){
				redisCacheService.delete(WORK_ORDER_HANDLER_QUEUE);
				return;
			}
	/*		Collections.sort(handlerList);	*/		
			
			 //将接口中获取的list数据装进队列，将改变队列头尾元素
			BlockingQueue<UserVO> handlerQueue=new LinkedBlockingQueue<UserVO>();
		    for(UserVO userVo:handlerList){
		        handlerQueue.put(userVo);
		    }
		    
		    //初始化队列头尾元素，若配置表中的当前处理人不为空，则当前处理人应处于列尾，
		    CussSystemConfig  cussSystemConfig =selectByName("workOrderHandlerUM");
		    if(cussSystemConfig!=null){
			//    UserVO userVO=new UserVO(cussSystemConfig.getValue());
			    for(int i=0;i<handlerQueue.size();i++){
			    	UserVO quser=handlerQueue.poll();
			    	handlerQueue.put(quser);
			    	if(quser.getUserUMID().equals(cussSystemConfig.getValue())){
			    		break;
			    	}
			    }
		    }
		    redisCacheService.put(WORK_ORDER_HANDLER_QUEUE, 0, handlerQueue);		
		    logger.info("method getLatesAlltWorkOrderHandlers result = {}", handlerQueue);
		}catch (Exception e) {
			logger.error("定时任务--获取最新所有客服专员信息异常：", e);
		}
	}

	@Override
	public int insertCussSystemConfig(CussSystemConfig record) {
		return cussSystemConfigMapper.insertCussSystemConfig(record);
	}
	
	public List<Map<String,Object>> selectByParentType(Integer parentTypeId){
		return cussWorkOrderTypeMapper.selectByParentType(parentTypeId);
	}

	@Override
	public List<Map<String, Object>> selectAllType() {	
		return cussWorkOrderTypeMapper.selectAllType();
	}

	@Override
	public List<CussWorkOrderDealflow> selectByIdWorkOrder(Page page,Integer idWorkOrder) {
		return cussWorkOrderDealflowMapper.selectByIdWorkOrder(page,idWorkOrder);
	}
	
	/**
	 * 处理中：
	 * 待处理或审核未通过工单，走处理中流程
	 * 处理流程：工单状态更改为处理中
	 */
	public void inHandCussWorkOrder(Map<String,String> paramMap) throws Exception{
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));
		CussWorkOrder cussWorkOrder=cussWorkOrderMapper.getCussWorkOrder(idWorkOrder);
		CussWorkOrderDealflow cussWorkOrderDealflow=new CussWorkOrderDealflow();		
		
		cussWorkOrderDealflow.setCreator(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCreatorName(paramMap.get("currHandlerName"));
		cussWorkOrderDealflow.setCreatorRole(paramMap.get("currHandlerRole"));
		cussWorkOrderDealflow.setCreatorType("1".equals(paramMap.get("channelSource"))?1:0);
		cussWorkOrderDealflow.setPreOperatorUm(cussWorkOrder.getCurrOperatorUm());
		cussWorkOrderDealflow.setCurrOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.InHand.getValue());
		cussWorkOrderDealflow.setPreWkordStatus(cussWorkOrder.getWorkOrderStatus());
		cussWorkOrderDealflow.setOperateFlowLog(String.format("工单由%s状态改为处理中状态", cussWorkOrder.getStatusName()));
		cussWorkOrderDealflow.setIdWorkOrder(idWorkOrder);
				
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);
		
		cussWorkOrder.setCurrOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrder.setWorkOrderStatus(WorkOrderStatus.InHand.getValue());
		cussWorkOrder.setLastUpdatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrder.setIdWorkOrder(idWorkOrder);		
		updateByPrimaryKey(cussWorkOrder);		
	}
	
	/**
	 * 提交：
	 * 待处理工单/处理中工单，提交处理建议
	 * 处理流程：提交处理建议,状态不变
	 */
	public void submitCussWorkOrder(Map<String,String> paramMap) throws Exception{
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));
		CussWorkOrderDealflow cussWorkOrderDealflow=cussWorkOrderDealflowMapper.selectLastedByIdWorkOrder(idWorkOrder);		
		
		cussWorkOrderDealflow.setIdDealFlow(null);
		cussWorkOrderDealflow.setCreator(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCreatorName(paramMap.get("currHandlerName"));
		cussWorkOrderDealflow.setCreatorRole(paramMap.get("currHandlerRole"));		
		cussWorkOrderDealflow.setPreWkordStatus(cussWorkOrderDealflow.getCurrWkordStatus());
		cussWorkOrderDealflow.setPreOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCurrOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setOperateFlowLog(paramMap.get("operateFlowLog"));
		cussWorkOrderDealflow.setIdWorkOrder(idWorkOrder);				
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);	
	}	
	
	/**
	 * 打回：
	 * 待处理工单，走工单打回流程
	 * 处理流程：针对web工单，工单状态返回到客户申请打回阶段,分配的处理人不变
	 * 生成的工单流程里面，当前工单处理人和上一次工单处理人是同一人？
	 */
	public void applyBackCussWorkOrder(Map<String,String> paramMap) throws Exception{
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));
		CussWorkOrder cussWorkOrder=new CussWorkOrder();
		CussWorkOrderDealflow cussWorkOrderDealflow=new CussWorkOrderDealflow();
		cussWorkOrder.setWorkOrderStatus(WorkOrderStatus.ApplyBack.getValue());
		cussWorkOrder.setLastUpdatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrder.setIdWorkOrder(idWorkOrder);
		
		cussWorkOrderDealflow.setCreator(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCreatorName(paramMap.get("currHandlerName"));
		cussWorkOrderDealflow.setCreatorRole(paramMap.get("currHandlerRole"));		
		cussWorkOrderDealflow.setCreatorType(1);//客户
		cussWorkOrderDealflow.setPreOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCurrOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.Pending.getValue());
		cussWorkOrderDealflow.setPreWkordStatus(WorkOrderStatus.ApplyBack.getValue());
		cussWorkOrderDealflow.setOperateFlowLog("打回了工单，并将处理结果反馈给了客户，反馈信息："+paramMap.get("feedbackToCustomer"));
		cussWorkOrderDealflow.setIdWorkOrder(idWorkOrder);
		
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);
		updateByPrimaryKey(cussWorkOrder);
	}
	
	/**
	 * 已完成：
	 * 处理中工单，走处理完成流程
	 * 处理流程：针对web工单，直接走提交审核流程，针对其他工单，走完成流程
	 */
	public void completedCussWorkOrder(Map<String,String> paramMap) throws Exception{
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));		
		CussWorkOrder cussWorkOrder=cussWorkOrderMapper.getCussWorkOrder(idWorkOrder);
		
		CussWorkOrderDealflow cussWorkOrderDealflow=new CussWorkOrderDealflow();
		cussWorkOrderDealflow.setPreWkordStatus(cussWorkOrder.getWorkOrderStatus());						
		cussWorkOrderDealflow.setPreOperatorUm(cussWorkOrder.getCurrOperatorUm());
		cussWorkOrderDealflow.setCreator(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCreatorName(paramMap.get("currHandlerName"));
		cussWorkOrderDealflow.setCreatorRole(paramMap.get("currHandlerRole"));		
		cussWorkOrderDealflow.setIdWorkOrder(idWorkOrder);
		
		if("1".equals(paramMap.get("channelSource"))){  //web工单
		    cussWorkOrder.setWorkOrderStatus(WorkOrderStatus.Audit.getValue());
		    cussWorkOrder.setIsApprove(Constant.ValidFlag.Y.toString());
		    cussWorkOrder.setCurrOperatorUm(null); //客户经理
		    cussWorkOrderDealflow.setCreatorType(1);//客户
		    cussWorkOrder.setFeedbackToCustomer(paramMap.get("feedbackToCustomer"));
			cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.Audit.getValue());	
			cussWorkOrderDealflow.setOperateFlowLog("提交工单以及客户反馈信息进行审核。客户反馈信息："+paramMap.get("feedbackToCustomer"));
			cussWorkOrder.setCurrOperatorUm(paramMap.get("currHandlerUm"));
		}
		else{
			cussWorkOrder.setCurrOperatorUm(paramMap.get("currHandlerUm"));
			cussWorkOrder.setFeedbackToCustomer(paramMap.get("feedbackToCustomer"));
			cussWorkOrder.setWorkOrderStatus(WorkOrderStatus.Completed.getValue());	
			
			cussWorkOrderDealflow.setCreatorType(0);//客户专员
			cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.Completed.getValue());
			cussWorkOrderDealflow.setCurrOperatorUm(paramMap.get("currHandlerUm"));	
			cussWorkOrderDealflow.setOperateFlowLog("已完成了工单，并将处理结果反馈给了客户，反馈信息："+paramMap.get("feedbackToCustomer"));
		}									
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);
		
		cussWorkOrder.setLastUpdatorUm(paramMap.get("currHandlerUm"));
		updateByPrimaryKey(cussWorkOrder);
	}
	
	/**
	 * 指派：
	 * 待处理工单/处理中工单，走指派流程
	 * 处理流程：重新设置处理人
	 * 处理状态不变
	 */
	public void appointCussWorkOrder(Map<String,String> paramMap) throws Exception{
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));				
		
		CussWorkOrderDealflow cussWorkOrderDealflow=cussWorkOrderDealflowMapper.selectLastedByIdWorkOrder(idWorkOrder);			
		cussWorkOrderDealflow.setCreator(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCreatorName(paramMap.get("currHandlerName"));
		cussWorkOrderDealflow.setCreatorRole(paramMap.get("currHandlerRole"));		
		cussWorkOrderDealflow.setCurrOperatorUm(paramMap.get("nextHandlerUm"));
		cussWorkOrderDealflow.setOperateFlowLog(String.format("指派工单给专员%s进行处理", paramMap.get("nextHandlerUm")));
		cussWorkOrderDealflow.setIdWorkOrder(idWorkOrder);		
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);
			
		CussWorkOrder cussWorkOrder=new CussWorkOrder();	
		cussWorkOrder.setIdWorkOrder(idWorkOrder);
	    cussWorkOrder.setCurrOperatorUm(paramMap.get("nextHandlerUm"));
	    cussWorkOrder.setLastUpdatorUm(paramMap.get("currHandlerUm"));
		updateByPrimaryKey(cussWorkOrder);
	}	
	
	
	/**
	 * 关闭：
	 * 待处理工单/处理中工单，走关闭流程
	 * 处理流程：设置工单为关闭状态
	 */
	public void closedCussWorkOrder(Map<String,String> paramMap) throws Exception{
		
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));
		
		CussWorkOrder cussWorkOrder=cussWorkOrderMapper.getCussWorkOrder(idWorkOrder);

		CussWorkOrderDealflow cussWorkOrderDealflow=new CussWorkOrderDealflow();
	    cussWorkOrderDealflow.setCreatorType("1".equals(paramMap.get("channelSource"))?1:0);
		cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.Closed.getValue());
		cussWorkOrderDealflow.setPreWkordStatus(cussWorkOrder.getWorkOrderStatus());			
		cussWorkOrderDealflow.setCreator(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCreatorName(paramMap.get("currHandlerName"));
		cussWorkOrderDealflow.setCreatorRole(paramMap.get("currHandlerRole"));		
		cussWorkOrderDealflow.setPreOperatorUm(cussWorkOrder.getCurrOperatorUm());
		cussWorkOrderDealflow.setOperateFlowLog("关闭了工单");
		cussWorkOrderDealflow.setCurrOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setIdWorkOrder(idWorkOrder);		
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);		
		
	    cussWorkOrder.setWorkOrderStatus(WorkOrderStatus.Closed.getValue());
	    cussWorkOrder.setCurrOperatorUm(paramMap.get("currHandlerUm"));
	    cussWorkOrder.setLastUpdatorUm(paramMap.get("currHandlerUm"));
	    cussWorkOrder.setFeedbackToCustomer(paramMap.get("feedbackToCustomer"));
	    cussWorkOrder.setIdWorkOrder(idWorkOrder);
		updateByPrimaryKey(cussWorkOrder);
	}
	
	/**
	 * 紧急程度：
	 * 待处理工单/处理中工单
	 * 处理流程：设置工单紧急度
	 */
	public void changUrgentCussWorkOrder(Map<String,String> paramMap) throws Exception{
		
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));
		
		CussWorkOrder cussWorkOrder=new CussWorkOrder();
		CussWorkOrder cussWorkOrderPre=cussWorkOrderMapper.getCussWorkOrder(idWorkOrder);
		
		if(cussWorkOrderPre.getUrgentPriority().intValue()==Integer.valueOf(paramMap.get("urgentPriority")).intValue())return;

		CussWorkOrderDealflow cussWorkOrderDealflow=new CussWorkOrderDealflow();
	    cussWorkOrderDealflow.setCreatorType("1".equals(paramMap.get("channelSource"))?1:0);
		cussWorkOrderDealflow.setPreWkordStatus(cussWorkOrderPre.getWorkOrderStatus());	
		cussWorkOrderDealflow.setCurrWkordStatus(cussWorkOrderPre.getWorkOrderStatus());
		cussWorkOrderDealflow.setCreator(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setCreatorName(paramMap.get("currHandlerName"));
		cussWorkOrderDealflow.setCreatorRole(paramMap.get("currHandlerRole"));		
		cussWorkOrderDealflow.setPreOperatorUm(cussWorkOrderPre.getCurrOperatorUm());
		cussWorkOrderDealflow.setOperateFlowLog(cussWorkOrderPre.getUrgentPriority().intValue()>Integer.valueOf(paramMap.get("urgentPriority")).intValue()?(String.format("紧急程度由%s降级为%s", cussWorkOrderPre.getUrgentName(),generateOperateFlowLog(Integer.valueOf(paramMap.get("urgentPriority"))))):(String.format("紧急程度由%s升级为%s", cussWorkOrderPre.getUrgentName(),generateOperateFlowLog(Integer.valueOf(paramMap.get("urgentPriority"))))));
		cussWorkOrderDealflow.setCurrOperatorUm(paramMap.get("currHandlerUm"));
		cussWorkOrderDealflow.setIdWorkOrder(idWorkOrder);		
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);		
		
	    cussWorkOrder.setUrgentPriority(Integer.valueOf(paramMap.get("urgentPriority")));
	    cussWorkOrder.setLastUpdatorUm(paramMap.get("currHandlerUm"));
	    cussWorkOrder.setIdWorkOrder(idWorkOrder);
		updateByPrimaryKey(cussWorkOrder);
	}	

	public String generateOperateFlowLog(Integer urgent){
		String urgentName="";
		switch(urgent){
		  case 10:{urgentName="低";break;}
		  case 30:{urgentName="一般";break;}
		  case 50:{urgentName="紧急";break;}
		  case 70:{urgentName="非常紧急";break;}
		}		
		return urgentName;
	}	
	
	/**
	 * 审核及重新打开：
	 * 走工单审核或重新打开流程
	 * 处理流程：工单状态根据审核结果而定。审核通过，审核不通过，已关闭工单修改为待处理状态
	 */
	public void auditCussWorkOrder(Map<String,String> paramMap) throws Exception{
		Integer idWorkOrder=Integer.valueOf(paramMap.get("idWorkOrder"));
		AppUser appUser=AuthenticationUtils.getCurrentUser();
		String dealType=paramMap.get("dealType");
		CussWorkOrder cussWorkOrder=cussWorkOrderMapper.getCussWorkOrder(idWorkOrder);
		CussWorkOrderDealflow cussWorkOrderDealflow=new CussWorkOrderDealflow();
		if("approved".equals(dealType)){
			cussWorkOrderDealflow.setCreatorType(1);//客户
			cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.Approved.getValue());
			cussWorkOrderDealflow.setOperateFlowLog("审核通过");
		}
		if("unapproved".equals(dealType)){
			cussWorkOrderDealflow.setCreatorType("1".equals(cussWorkOrder.getChannelSource())?1:0);//客户
			cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.UnApproved.getValue());
			cussWorkOrderDealflow.setOperateFlowLog("审核未通过，反馈信息："+paramMap.get("feedbackToCustomer"));			
		}
		if("reOpen".equals(dealType)){
			cussWorkOrderDealflow.setCreatorType(1);//客户
			cussWorkOrderDealflow.setCurrWkordStatus(WorkOrderStatus.Pending.getValue());
			cussWorkOrderDealflow.setOperateFlowLog("已关闭工单被重新打开，反馈信息："+paramMap.get("feedbackToCustomer"));        
		}
	    
	    cussWorkOrderDealflow.setPreWkordStatus(cussWorkOrder.getWorkOrderStatus());
		cussWorkOrderDealflow.setPreOperatorUm(cussWorkOrder.getCurrOperatorUm());
		cussWorkOrderDealflow.setCurrOperatorUm(appUser.getUserUMID());
		cussWorkOrderDealflow.setCreator(appUser.getUserUMID());	
		cussWorkOrderDealflow.setCreatorName(appUser.getUsername());
		cussWorkOrderDealflow.setCreatorRole(appUser.getUserRole());
		cussWorkOrderDealflow.setIdWorkOrder(cussWorkOrder.getIdWorkOrder());		
		cussWorkOrderDealflowMapper.insertSelective(cussWorkOrderDealflow);	
		
		cussWorkOrder.setWorkOrderStatus("approved".equals(dealType)?WorkOrderStatus.Approved.getValue():("unapproved".equals(dealType)?WorkOrderStatus.UnApproved.getValue():WorkOrderStatus.Pending.getValue()));	    
		cussWorkOrder.setLastUpdatorUm(appUser.getUserUMID());
		updateByPrimaryKey(cussWorkOrder);
	}			
}
